<?php
error_reporting(E_ALL ^ E_NOTICE);

set_time_limit(0);
date_default_timezone_set("PRC"); //timezone

$db1=mysql_connect("localhost","root","root"); 
mysql_select_db('faithwar',$db1); 

$db2=mysql_connect('192.168.0.17','root','root'); 
mysql_select_db('faithwar',$db2); 

$dateTime = date("Y-m-d",strtotime("-1 day"));
#$dateTime = date("Y-m-d");
//$dateTime ='2012-08-09';//test

    $deviceid_query = mysql_query("select  DISTINCT deviceid from tbheartbeat where date = '$dateTime'",$db2); //id
    //$deviceid = mysql_fetch_array($deviceid_query);
    //while($deviceid[deviceid])

while($deviceid = mysql_fetch_array($deviceid_query))
{
        //开始结束时间
    $Start_query = mysql_query("SELECT *
        FROM `tbheartbeat`
        WHERE `deviceid` = '$deviceid[deviceid]'
        AND date = '$dateTime'
        LIMIT 1",$db2);

    $Start = mysql_fetch_array($Start_query);

    $end_query = mysql_query("SELECT *
        FROM `tbheartbeat`
        WHERE `deviceid` = '$deviceid[deviceid]'
        AND date = '$dateTime'
        ORDER BY logDate DESC
        LIMIT 1",$db2);

    $end = mysql_fetch_array($end_query);


    $time1_query = mysql_query("SELECT UNIX_TIMESTAMP( '$end[0]' ) - UNIX_TIMESTAMP( '$Start[0]' )");
    $time1 = mysql_fetch_array($time1_query);
        //每用户加10秒
    $time_query = mysql_query("SELECT '$time1[0]' + '10'");
    $time = mysql_fetch_array($time_query);

     $sql = "UPDATE  faithwar_query_temp SET dayTotalTime= '$time[0]' WHERE deviceid= '$deviceid[deviceid]' and  date ='$dateTime'";
     
   if(mysql_query($sql,$db1)){
    echo "1 update is ok!";
} else {
    echo "failed".mysql_error();
}

  
}

mysql_close($db1);
mysql_close($db2);
?>